$(document).ready(function() {
	$("#submit").click(send_message);
	$(document).keydown(function(){
		if(event.ctrlKey && (event.keyCode === 13 || event.keyCode === 10)){
			send_message();
		}
	});
	$("#load").click(function() {
		$.ajax({
			url: 'php/interface.php?action=load_message',
			method: 'post',
			dataType: 'json',
		}).done(function(data) {
			if(data.result === true) {
				var receive_msg = $('#receive_msg').html();
				var new_msg = parse_message(data.msg);
				console.log(new_msg);
				var message_str = "";
				for(var msg in new_msg[0])
				{
					message_str += new_msg[0][msg];	
				}
				$("#receive_msg").html(receive_msg + message_str);
			}
		});
	});
	(function load_message(){
		$.ajax({
			url: 'php/interface.php?action=load_message',
			method: 'post',
			dataType: 'json',
			timeout: 4000,
			error: function(jqXHR, textStatus, errorThrown){
					load_message();
				},
			success: function(data, textStatus, jqXHR){
					if(data.result === true) {
						var receive_msg = $('#box1').html();
						console.log(data.msg);
						var new_msg = parse_message(data.msg);
						console.log(new_msg);
						var message_str = "";
						for(var msg in new_msg[0])
						{
							message_str += new_msg[0][msg];	
						}
						$("#box1").html(receive_msg + message_str);
						$("#chart").html(new_msg[2]);
					}
					load_message();
				}
			});
	})();
	

	
	$(window).unload(function(){
		$.ajax({
			url: 'php/interface.php?action=quit',
			method: 'post'
		})
	});
});


function load_message_test(){
	var sum = $('chart').children().length;
	$.ajax({
		url: 'php/interface.php?action=load_message',
		method: 'post',
		dataType: 'json',
		timeout: 4000,
		error: function(jqXHR, textStatus, errorThrown){
				// load_message();
			},
		success: function(data, textStatus, jqXHR){
				if(data.result === true) {
					var receive_msg = $('#box1').html();
					console.log(data.msg);
					var new_msg = parse_message(data.msg);
					console.log(new_msg);
					var message_str = "";
					for(var msg in new_msg[0])
					{
						message_str += new_msg[0][msg];	
					}
					$("#box1").html(receive_msg + message_str);
					$("#chart").html(new_msg[2]);
				}
				// load_message();
			}
		});
};
//load_message();

function abc()
{
	$.ajax({
		// url: 'php/interface.php?action=get_online_users',
		url: 'php/test.php',
		method: 'post',
		dataType: 'json',
		timeout: 8000,
		success: function(data, textStatus, jqXHR){
				if(data.result === true) {
					console.log(data.msg);
					var online_list = load_users(data.msg);
					console.log(online_list);
					$("#on_line").html(online_list);
				}
			}
	})
}

// abc();

function new_message_animation () {
	$(".new_message").animate({opacity: '0.5'});
	$(".new_message").animate({opacity: '1.0'});
}

// var loop = self.setInterval(new_message_animation, 10);
new_message_animation();


function parse_message(data)
{
	var msg = new Array(3);
	msg[0] = new Array();
	msg[1] = new Array();
	// console.log(msg);
	
	console.log(data);
	var username = data.username;
	for(var i in data.message)
	{
		if(data.message[i]['username'] === username)
		{
			// console.log("admin");
			s_msg = "<div class='sendbox'>";
			s_msg = s_msg + "<div class='name'><span class='username'>" + data.message[i]['username'];
			s_msg = s_msg + "</span> <span class='pubdate'>" + data.message[i]['pubdate'];
			s_msg = s_msg + "</span></div><div class='send'><div class='arrow'></div>" + data.message[i]['content'];
			s_msg = s_msg + "</div></div>";
		}
		else
		{
			s_msg = "<div class='sendleftbox'>";
			s_msg = s_msg + "<div class='name'><span class='username'>" + data.message[i]['username'];
			s_msg = s_msg + "</span> <span class='pubdate'>" + data.message[i]['pubdate'];
			s_msg = s_msg + "</span></div><div class='sendleft'><div class='arrowleft'></div>" + data.message[i]['content'];
			s_msg = s_msg + "</div></div>";
		}

		console.log(s_msg);
		if(data.message[i]['from'] === '0')
		{
			msg[0].push(s_msg);
		}
		else
		{
			msg[1].push(s_msg);
		}
	}
	var str_self = "";
	var str_user = "";
	for(var i in data.users)
	{
		if (data.users[i]['username'] === username) 
		{
			str_self = "<p class='self'>" + data.users[i]['username'] + "</p>";
		}
		else
		{
			str_user = str_user + "<p class='user'>" + data.users[i]['username'] + "</p>";
		}
	}
	msg[2] = str_self + str_user;
	console.log(msg);

	return msg;
}

function send_message(){
	var message = $("#ta").val();
	if(message.length === 0){
		alert("信息不能为空!");
	}
	else{
		$.ajax({
			url: 'php/interface.php?action=send_message',
			method: 'post',
			dataType: 'json',
			data: {
				message: message,
				mes_to: 0
			}
		}).done(function(data) {
			<!-- if (data.result === true) { -->
			<!-- 	location.href = 'index.html'; -->
			<!-- } else { -->
			<!-- 	alert(data.msg); -->
			<!-- } -->
		});
	}
	$("#ta").val("");
}